
package models.spider.actor;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import models.spider.stats.Stopwatch;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

/**
 * 
 * @author laurent
 * 
 */
public class Status extends UntypedActor {

    public static class Details {

        private final List<Event> events = new ArrayList<>();
        private int threads = 0;

        public Details(final Status actor) {
            this.events.addAll(actor.events.items);
        }
        
        public Details() {
        	
        }

        public List<Event> getEvents() {
            return events;
        }

		public int getThreads() {
			return threads;
		}

		public void setThreads(int threads) {
			this.threads = threads;
		}

    }

    public static class EventQueue {

        protected int               capacity = 10;
        protected LinkedList<Event> items    = new LinkedList<>();

        public EventQueue(final int capacity) {
            this.capacity = capacity;
        }

        public EventQueue() {
            this(10);
        }

        public int size() {
            return this.items.size();
        }

        public int capacity() {
            return capacity;
        }

        public boolean isFull() {
            return size() >= capacity;
        }

        public boolean add(final Event e) {
            if (isFull()) {
                items.poll();
            }
            return items.add(e);
        }

    }

    protected LoggingAdapter log    = null;
    protected EventQueue     events = new EventQueue();

    public static class Message {

    }

    public static class Event extends Message {

        protected String url       = null;
        protected Date   date      = null;
        protected long   connected = 0;
        protected long   parsed    = 0;
        protected long   total     = 0;

        Event(final String url, final Long connected, final Long parsed, final Long total) {
            this.url = url;
            this.date = new Date();
            this.connected = connected;
            this.parsed = parsed;
            this.total = total;
        }

        Event(final String url, final Stopwatch connected, final Stopwatch parsed, final Stopwatch total) {
            this.url = url;
            this.date = new Date();
            this.connected = connected.elapsed();
            this.parsed = parsed.elapsed();
            this.total = total.elapsed();
        }

        public String getUrl() {
            return url;
        }

        public Date getDate() {
            return date;
        }

        public long getConnected() {
            return connected;
        }

        public long getParsed() {
            return parsed;
        }

        public long getTotal() {
            return total;
        }

    }

    public static class Info extends Message {

    }

    Status() {
        log = Logging.getLogger(getContext().system(), this);
    }

    @Override
    public void onReceive(final Object message) throws Exception {
        if (message instanceof Info) {
            info((Info) message);
        } else if (message instanceof Event) {
            event((Event) message);
        } else {
            unhandled(message);
        }

        // log.debug("index onReceive done");
    }

    protected void info(final Info message) {
    	getSender().tell(new Details(this), getSelf());
    }

    protected void event(final Event message) {
        events.add(message);
    }

    @Override
    public void preStart() {
        // log.debug("index pre start");
    }

    @Override
    public void postStop() {
        // log.debug("index post stop");
    }

}
